package com.zhuangjie.order.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.zhuangjie.mvc.helper.BigDecimalHelper;
import com.zhuangjie.order.constant.OrderConstant;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.math.BigDecimal;

/**
 * <p>
 * 订单金额详细表
 * </p>
 *
 * @author zhuangjie
 * @since 2024-03-30
 */
@Getter
@Setter
@Accessors(chain = true)
@TableName("order_amount")
@ApiModel(value = "OrderAmount对象", description = "订单金额详细表")
public class OrderAmount implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty("订单金额汇总id")
    @TableId(value = "id", type = IdType.ASSIGN_ID)
    private Long id;

    @ApiModelProperty("订单id")
    @TableField("order_id")
    private Long orderId;

    @ApiModelProperty("总菜品金额")
    @TableField("total_dish_amount")
    private BigDecimal totalDishAmount;

    @ApiModelProperty("商家预配置配送费")
    @TableField("preset_delivery_fee")
    private BigDecimal presetDeliveryFee;

    @ApiModelProperty("实际的配送费")
    @TableField("actual_delivery_fee")
    private BigDecimal actualDeliveryFee;

    @ApiModelProperty("打包费")
    @TableField("packing_fee")
    private BigDecimal packingFee;

    @ApiModelProperty("店铺活动优惠金额")
    @TableField("shop_activity_discount_amount")
    private BigDecimal shopActivityDiscountAmount;

    @ApiModelProperty("店铺优惠券优惠金额")
    @TableField("shop_coupon_discount_amount")
    private BigDecimal shopCouponDiscountAmount;

    @ApiModelProperty("平台券优惠金额")
    @TableField("platform_coupon_discount_amount")
    private BigDecimal platformCouponDiscountAmount;

    @ApiModelProperty("店铺需承担的平台券优惠金额")
    @TableField("shop_borne_platform_coupon_discount_amount")
    private BigDecimal shopBornePlatformCouponDiscountAmount;

    @ApiModelProperty("用户支付金额")
    @TableField("user_payment_amount")
    private BigDecimal userPaymentAmount;

    @ApiModelProperty("商家订单实际收入金额")
    @TableField("order_shop_income")
    private BigDecimal orderShopIncome;

    @ApiModelProperty("系统抽取的金额")
    @TableField("platform_revenue")
    private BigDecimal platformRevenue;

    @ApiModelProperty("订单商家利润")
    @TableField("order_shop_profit")
    private BigDecimal orderShopProfit;

    public void calculateAndSetDynamicProperties() {
        calculateAndSetUserPaymentAmount();
        // 下面会调用calculateAndSetOrderShopIncome
        calculateAndSetOrderShopProfit();
    }


    /**
     * 计算用户支付金额
     */
    public BigDecimal calculateAndSetUserPaymentAmount() {
        BigDecimal amount = BigDecimalHelper.longOperation(this.totalDishAmount)
                .add(this.presetDeliveryFee)
                .add(this.packingFee)
                .subtract(this.shopActivityDiscountAmount)
                .subtract(this.shopCouponDiscountAmount)
                .subtract(this.platformCouponDiscountAmount)
                .getResult();
        return this.userPaymentAmount = amount;
    }


    /**
     * 计算订单商家收入
     */
    public BigDecimal calculateAndSetOrderShopIncome() {
        BigDecimal income = BigDecimalHelper.longOperation(this.totalDishAmount)
                .add(this.presetDeliveryFee)
                .add(this.packingFee)
                .subtract(this.shopActivityDiscountAmount)
                .subtract(this.shopCouponDiscountAmount)
                .subtract(this.shopBornePlatformCouponDiscountAmount)
                .getResult();

        return this.orderShopIncome = income;
    }

    /**
     * 计算平台抽成
     */
    public BigDecimal calculatePlatformRevenue() {
        BigDecimal unpaidOrderShopProfit = BigDecimalHelper.longOperation(calculateAndSetOrderShopIncome())
                .subtract(actualDeliveryFee)
                .getResult();
        return this.platformRevenue = BigDecimalHelper.multiplicationOperation(unpaidOrderShopProfit, OrderConstant.PLATFORM_ORDER_PERCENTAGE);
    }

    /**
     * 计算订单商家利润
     */
    public BigDecimal calculateAndSetOrderShopProfit() {
        BigDecimal platformRevenue = calculatePlatformRevenue();
        // 动态属性orderShopIncome的值在calculatePlatformRevenue中已经算出来了，这里是有值的
        return this.orderShopProfit = BigDecimalHelper.subtractionOperation(orderShopIncome,actualDeliveryFee, platformRevenue);
    }
}
